package com.zhouheng.springbootdesignpattern.proxypattern.jdkdynamicproxy;

import com.zhouheng.springbootdesignpattern.proxypattern.staticproxy.IUserDao;
import com.zhouheng.springbootdesignpattern.proxypattern.staticproxy.UserDaoImpl;

/**
 * 测试jdk动态代理
 *
 * @author 周恒
 * @date 20190610 16:52:42
 * @since v1.0
 */
public class TestJdkDynamicProxy {

    public static void main(String[] args) {
        testUserDao();
        testStudentDao();
    }

    private static void testStudentDao() {
        // 目标对象
        IStudentDao target = new StudentDaoImpl();
        // 【原始的类型 class com.zhouheng.springbootdesignpattern.proxypattern.jdkdynamicproxy.StudentDaoImpl】
        System.out.println(target.getClass());

        // 给目标对象，创建代理对象
        IStudentDao proxy = (IStudentDao) new ProxyFactory(target).getProxyInstance();
        // class class com.sun.proxy.$Proxy0   内存中动态生成的代理对象
        System.out.println(proxy.getClass());

        // 执行方法   【代理对象】
        proxy.save("张三", 18);
        proxy.update("王五");
    }

    private static void testUserDao() {
        // 目标对象
        IUserDao target = new UserDaoImpl();
        // 给目标对象，创建代理对象
        IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance();
        proxy.save();
    }

}
